package com.health.healthSys.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.health.common.core.domain.entity.SysUser;
import com.health.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.health.common.annotation.Log;
import com.health.common.core.controller.BaseController;
import com.health.common.core.domain.AjaxResult;
import com.health.common.enums.BusinessType;
import com.health.healthSys.domain.HealthSport;
import com.health.healthSys.service.IHealthSportService;
import com.health.common.utils.poi.ExcelUtil;
import com.health.common.core.page.TableDataInfo;

/**
 * 运动记录Controller
 */
@RestController
@RequestMapping("/healthSys/sport")
public class HealthSportController extends BaseController {

    @Autowired
    private IHealthSportService healthSportService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询运动记录列表
     */
    @PreAuthorize("@ss.hasPermi('healthSys:sport:list')")
    @GetMapping("/list")
    public TableDataInfo list(HealthSport healthSport) {
        startPage();
        List<HealthSport> list = healthSportService.selectHealthSportList(healthSport);
        return getDataTable(list);
    }

    /**
     * 导出运动记录列表
     */
    @PreAuthorize("@ss.hasPermi('healthSys:sport:export')")
    @Log(title = "运动记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HealthSport healthSport) {
        List<HealthSport> list = healthSportService.selectHealthSportList(healthSport);
        ExcelUtil<HealthSport> util = new ExcelUtil<HealthSport>(HealthSport.class);
        util.exportExcel(response, list, "运动记录数据");
    }

    /**
     * 获取运动记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('healthSys:sport:query')")
    @GetMapping("/{sportId}")
    public AjaxResult getInfo(@PathVariable("sportId") String sportId) {
        return success(healthSportService.selectHealthSportBySportId(sportId));
    }

    /**
     * 新增运动记录（管理端）
     */
    @PreAuthorize("@ss.hasPermi('healthSys:sport:add')")
    @Log(title = "运动记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HealthSport healthSport) {
        completeUserInfo(healthSport);
        return toAjax(healthSportService.insertHealthSport(healthSport));
    }

    /**
     * 修改运动记录（管理端）
     */
    @PreAuthorize("@ss.hasPermi('healthSys:sport:edit')")
    @Log(title = "运动记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HealthSport healthSport) {
        SysUser sysUser=sysUserService.selectUserById(healthSport.getUserId());
        healthSport.setUserName(sysUser.getUserName());
        return toAjax(healthSportService.updateHealthSport(healthSport));
    }

    /**
     * 用户自助修改记录
     */

    @PutMapping("/userEdit")
    public AjaxResult userEdit(@RequestBody HealthSport healthSport) {
        return toAjax(healthSportService.updateHealthSport(healthSport));
    }

    /**
     * 删除运动记录
     */
    @PreAuthorize("@ss.hasPermi('healthSys:sport:remove')")
    @Log(title = "运动记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sportIds}")
    public AjaxResult remove(@PathVariable String[] sportIds) {
        return toAjax(healthSportService.deleteHealthSportBySportIds(sportIds));
    }

    /**
     * 补全用户信息
     */
    private void completeUserInfo(HealthSport healthSport) {
        if (healthSport.getUserId() == null) {
            healthSport.setUserId(getUserId());
            SysUser currentUser = getLoginUser().getUser();
            healthSport.setUserName(currentUser.getUserName());
        } else {
            SysUser user = sysUserService.selectUserById(healthSport.getUserId());
            if (user != null) {
                healthSport.setUserName(user.getUserName());
            }
        }
    }
}
